package com.ITArmy.controller;

import com.ITArmy.beans.Blog;
import com.ITArmy.beans.Comment;
import com.ITArmy.beans.Question;
import com.ITArmy.beans.Shows.CommentMessage;
import com.ITArmy.beans.Type;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.service.CommentService;
import com.ITArmy.service.QuestionService;
import com.ITArmy.service.UserService;
import com.ITArmy.util.GetUserSession;
import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Api(value = "问题接口层次", tags = "问题接口层次Controller层")
@Controller
@RequestMapping("/question")
public class QuestionController extends BaseController {

    @RequestMapping("/add")
    @ApiOperation(value = "进入问题编辑页面，",
            httpMethod = "GET", response = ServerResponse.class)
    public String add(Model model) {
        model.addAttribute("type",typeService.typeList().getData());
        return "askpage";
    }

    @RequestMapping("/ask")
    @ApiOperation(value = "进入问题主页，",
            httpMethod = "GET", response = ServerResponse.class)
    public String goIndex(Model model) {
        questionService.goIndex(model);
        return "ask";
    }

    @RequestMapping("/findQuestionByTitle")
    @ApiOperation(value = "根据标题模糊查询，",
            httpMethod = "POST", response = ServerResponse.class)
    public String findQuestionByTitle(String title, Model model) {
        ServerResponse questionServerResponse = questionService.findQuestionByTitle(title);
        model.addAttribute("questionListByTitle", questionServerResponse);
        return "ask";
    }

    @RequestMapping("/findQuestionByType")
    @ApiOperation(value = "根据问题类型搜索，", notes = "类型用固定选择",
            httpMethod = "POST", response = ServerResponse.class)
    public String findQuestionByType(Integer tid, Model model) {
        ServerResponse questionServerResponse = questionService.findQuestionByType(tid);
        model.addAttribute("questionListByType", questionServerResponse);
        return "";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "用户删除问题信息",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse delete(Long id) {
        return questionService.deleteQuestionById(id);
    }


    @RequestMapping("/addQuestion")
    @ResponseBody
    @ApiOperation(value = "用户发表问题", httpMethod = "POST")
    public ServerResponse addQuestion(HttpServletRequest request,Question question) {
        return questionService.publishQuestion(request,question);
    }

    @RequestMapping("/commentQuestion")
    @ResponseBody
    @ApiOperation(value = "用户评论（回答）问题", httpMethod = "POST")
    public ServerResponse commentQuestion(HttpServletRequest request, Comment comment,Model model) {
        return commentService.comment(request,comment,model);
    }

    @RequestMapping("/adoptComment")
    @ResponseBody
    @ApiOperation(value = "用户采纳回答", httpMethod = "POST")
    public ServerResponse adoptComment(HttpServletRequest request,long ruid,long qid,long cid) {
        return questionService.adoptComment(request,ruid,qid,cid);
    }

    @RequestMapping("/readQuestion")
    @ApiOperation(value = "用户看问题", httpMethod = "GET", response = String.class)
    public String readBlog(HttpServletRequest request,long qid, Model model) {
        model.addAttribute("question",questionService.findQuestionById(request,qid));
        //查询对问题的评论(全部);
        List<CommentMessage> commentMessages = questionService.getCommentMessage(qid);
        for (int i = 0; i <commentMessages.size() ; i++) {
            System.out.println(commentMessages.get(i).getReplyMessage());
        }
        Collections.reverse(commentMessages);
        model.addAttribute("commentMessage", commentMessages);
        return "askArticle";
    }
}
